package com.bayes.teamshow.controller;

import com.alibaba.fastjson.JSONObject;
import com.bayes.teamshow.dao.ArticleRepository;
import com.bayes.teamshow.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
//@ResponseBody
@RequestMapping("/art")
public class ArticleController {
    @RequestMapping("shiyan")
    public String getput(){
        return "shiyan";
    }
    @Autowired
    private ArticleRepository articleRepository;
    @RequestMapping("/markdown")
    public String getMarkdown(){return "markdown";}
    @RequestMapping("/artsubsuccess")
    public String getartsubsuccess()
    {
        return "artsubsuccess";
    }

    @RequestMapping("/articleadd")
    @ResponseBody
    public Object getArticle(HttpServletRequest httpServletRequest){
        String title = httpServletRequest.getParameter("title");
        String description = httpServletRequest.getParameter("description");
        String content = httpServletRequest.getParameter("content");

        Article article = new Article();
        article.setTitle(title);
        article.setDescribes(description);
        article.setCreatedDate(new Date());
        // article.setCommentCount(0);
        article.setContent(content);
        articleRepository.save(article);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","true");
        jsonObject.put("url","artsubsuccess");
        return jsonObject;
        // return jsonObject;
    }
        //article.setCategory(category);
//        int articleId = articleService.addArticle(article);
//
//      //  String[] tags = tag.split(",");
//        for (String t : tags){
//            Tag tag1 = tagService.selectByName(t);
//            if (tag1==null){
//                Tag tag2 = new Tag();
//                tag2.setName(t);
//                tag2.setCount(1);
//                int tagId = tagService.addTag(tag2);
//
//                ArticleTag articleTag = new ArticleTag();
//                articleTag.setTagId(tagId);
//                articleTag.setArticleId(articleId);
//                tagService.addArticleTag(articleTag);
//            }else {
//                tagService.updateCount(tag1.getId(),tag1.getCount()+1);
//
//                ArticleTag articleTag = new ArticleTag();
//                articleTag.setTagId(tag1.getId());
//                articleTag.setArticleId(articleId);
//                tagService.addArticleTag(articleTag);
//            }


//
//        String categoryKey = RedisKeyUntil.getCategoryKey(category);
//        jedisService.incr(categoryKey);

      // return    articleRepository.save(article);
   // }
    @DeleteMapping(value="/article/{id}")
    public void userDelete(@PathVariable("id")Integer id){
        articleRepository.delete(id);
    }
    @GetMapping(value="/article/articlename/{articlename}")
    public Article articlefindById(@PathVariable("id")Integer id){
        return articleRepository.findOne(id);
    }
    @PutMapping(value="/article/{id}")
    public Article articleUpdate(@PathVariable("id")Integer id,
                           @RequestParam("title")String title,
                           @RequestParam("description")String description,@RequestParam("content")String content){
        Article article=new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        article.setDescribes(description);
        return articleRepository.save(article);
    }
    @RequestMapping("/articlelist")

    public String getarticleList()
    {
        return "postlist";
    }
    @RequestMapping("/getarticlelist")
    @ResponseBody
    public Object articlefindByCreatename(HttpServletRequest httpServletRequest){
        String  userid = httpServletRequest.getParameter("userid");
        List<Article> list= articleRepository.findByCreatename(userid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("articles",list);
        return jsonObject;
    }
}
